<?php

declare(strict_types=1);

namespace App\Admin\Controller\System\Permission;

use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Request\System\User\SystemUserCreateRequest;
use App\Admin\Request\System\User\SystemUserHompPageRequest;
use App\Admin\Request\System\User\SystemUserPasswordRequest;
use App\Admin\Request\System\User\SystemUserStatusRequest;
use App\Admin\Request\System\User\SystemUserUpdateRequest;
use App\Admin\Service\System\SystemUserService;
use DI\Attribute\Inject;
use DI\DependencyException;
use DI\NotFoundException;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\DeleteMapping;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\Annotation\Attributes\Route\PutMapping;
use Newbie\NewbieCollection;
use Newbie\NewbieController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use support\Request;
use support\Response;

/**
 * Class UserController
 * @package App\system\Controller
 */
#[Controller(prefix: "system/user"), Auth]
class UserController extends NewbieController {
    #[Inject]
    protected SystemUserService $service;

    /**
     * 用户列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("index"), Permission("system:user:index")]
    public function index (Request $request): Response {
        return $this->success($this->service->getPageList($request->all(), false));
    }

    /**
     * 回收站列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("recycle"), Permission("system:user:recycle")]
    public function recycle (Request $request): Response {
        return $this->success($this->service->getPageListByRecycle($request->all()));
    }

    /**
     * 新增一个用户
     * @param SystemUserCreateRequest $request
     * @return Response
     */
    #[PostMapping("save"), Permission("system:user:save")]
    public function save (SystemUserCreateRequest $request): Response {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 获取一个用户信息
     * @param int $id
     * @return Response
     */
    #[GetMapping("read/{id}"), Permission("system:user:read")]
    public function read (int $id): Response {
        return $this->success($this->service->read($id));
    }

    /**
     * 更新一个用户信息
     * @param int $id
     * @param SystemUserUpdateRequest $request
     * @return Response
     */
    #[PutMapping("update/{id}"), Permission("system:user:update")]
    public function update (int $id, SystemUserUpdateRequest $request): Response {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量删除用户到回收站
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("delete/{ids}"), Permission("system:user:delete")]
    public function delete (string $ids): Response {
        return $this->service->delete($ids) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量真实删除用户 （清空回收站）
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("realDelete/{ids}"), Permission("system:user:realDelete")]
    public function realDelete (string $ids): Response {
        return $this->service->realDelete($ids) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量恢复在回收站的用户
     * @param String $ids
     * @return Response
     */
    #[PutMapping("recovery/{ids}"), Permission("system:user:recovery")]
    public function recovery (string $ids): Response {
        return $this->service->recovery($ids) ? $this->success() : $this->error();
    }

    /**
     * 更改用户状态
     * @param SystemUserStatusRequest $request
     * @return Response
     */
    #[PutMapping("changeStatus"), Permission("system:user:changeStatus")]
    public function changeStatus (SystemUserStatusRequest $request): Response {
        return $this->service->changeStatus((int)$request->input('id'), (string)$request->input('status'))
            ? $this->success() : $this->error();
    }

    /**
     * 清除用户缓存
     * @param Request $request
     * @return Response
     * @throws DependencyException
     * @throws NotFoundException
     */
    #[PostMapping("clearCache"), Permission("system:user:cache")]
    public function clearCache (Request $request): Response {
        $this->service->clearCache((string)$request->input('id', null));
        return $this->success();
    }

    /**
     * 设置用户首页
     * @param SystemUserHompPageRequest $request
     * @return Response
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("setHomePage"), Permission("system:user:homePage")]
    public function setHomePage (SystemUserHompPageRequest $request): Response {
        return $this->service->setHomePage($request->validated()) ? $this->success() : $this->error();
    }

    /**
     * 初始化用户密码
     * @param int $id
     * @return Response
     */
    #[PutMapping("initUserPassword/{id}"), Permission("system:user:initUserPassword")]
    public function initUserPassword (int $id): Response {
        return $this->service->initUserPassword($id) ? $this->success() : $this->error();
    }

    /**
     * 更改用户资料，含修改头像 (不验证权限)
     * @param Request $request
     * @return Response
     * @throws DependencyException
     * @throws NotFoundException
     */
    #[PostMapping("updateInfo"), Auth]
    public function updateInfo (Request $request): Response {
        return $this->service->updateInfo($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 修改密码 (不验证权限)
     * @param SystemUserPasswordRequest $request
     * @return Response
     */
    #[PostMapping("modifyPassword")]
    public function modifyPassword (SystemUserPasswordRequest $request): Response {
        return $this->service->modifyPassword($request->validated()) ? $this->success() : $this->error();
    }

    /**
     * 用户导出
     * @param Request $request
     * @return Response
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("export"), Permission("system:user:export")]
    public function export (Request $request): Response {
        return $this->service->export($request->all(), \app\Admin\System\Dto\UserDto::class, '用户列表');
    }

    /**
     * 用户导入
     * @return Response
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("import"), Permission("system:user:import")]
    public function import (): Response {
        return $this->service->import(\App\Admin\Dto\System\UserDto::class) ? $this->success() : $this->error();
    }

    /**
     * 下载导入模板
     * @return Response
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("downloadTemplate")]
    public function downloadTemplate (): Response {
        return (new NewbieCollection)->export(\App\Admin\Dto\System\UserDto::class, '模板下载', []);
    }

    /**
     * 清除自己缓存
     * @return Response
     * @throws DependencyException
     * @throws NotFoundException
     */
    #[PostMapping("clearSelfCache")]
    public function clearSelfCache (): Response {
        return $this->service->clearCache(user()->getId()) ? $this->success() : $this->error();
    }
}